์คํ๋ง ์ ๋ฌธ - ์ฝ๋๋ก ๋ฐฐ์ฐ๋ ์คํ๋ง ๋ถํธ, ์น MVC, DB ์ ๊ทผ ๊ธฐ์ ์ ๋ค์ผ๋ฉฐ ์ ๋ฆฌํ๋ POST์ ๋๋ค.
์ ์ฒด์ ์ธ ํ๋ฆ
- Spring Project ์์ฑ
- Spring boot๋ก ์น ์๋ฒ ์คํ
- ํ์ ๋๋ฉ์ธ ๊ฐ๋ฐ
- ์น MVC ๊ฐ๋ฐ
- DB ์ฐ๋ - JDBC, JPA, Spring data JPA
- ํ ์คํธ ์ผ์ด์ค ์์ฑ
๐ ์ ์ ์ปจํ ์ธ
โ ์ ์ ์ปจํ ์ธ ๋?
- ์๋ฒ์ ์ ์ฅ๋์ด ์๊ณ ๋ณํ ์์ด ๋ธ๋ผ์ฐ์ ๋ก ๋ฟ๋ ค์ง๋ ์ปจํ ์ธ (ํ์ผ)
- ์์ฒญ์ ๋ฐ๋ผ ์๋ต๋ง์ ์ํ
Spring Boot๋ ์ ์ ์ปจํ
์ธ ๊ธฐ๋ฅ ์๋ ์ ๊ณต
โ https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-static-content
/staticํด๋์์ ์ ์ ์ปจํ ์ธ ๋ฅผ ์ฐพ์์ ์ ๊ณตํ๋ค.
/static ์ hello-static.html ์ ์์ฑ ๋ฐ ์์ฑํด๋ณด์.


- ํ์ผ ์์ฑ ํ ์๋ฒ๋ฅผ ๋์์ํค๋ฉด, http://localhost:8080/hello-static.html ์์ ์์ฑํ ์ ์ ์ปจํ
์ธ ์ธ
htmlํ์ผ์ ํ์ธํ ์ ์๋ค.
๐ MVC์ ํ ํ๋ฆฟ ์์ง
โ ํ ํ๋ฆฟ ์์ง์ด๋?
- ์๋ฒ์์ ๋์ ์ผ๋ก ํ์ผ์ ๋ฐ๊ฟ์ ๋ธ๋ผ์ฐ์ ๋ก ๋ฟ๋ ค์ฃผ๋ ์์ง
- jsp, php ๋ฑ์ด ์๋ค.
- ์ด๋ฅผ ์ํด ํ์ํ Model, View, Controller๋ฅผ ํฉ์ณ MVC๋ผ๊ณ ํ๋ค.
-
์์ ์๋ View๋ง์ผ๋ก ๊ฐ๋ฐ์ ์งํํ๋ค๋ฉด, ์ด์ ๋ View๋ ํ๋ฉด์ ๊ทธ๋ฆฌ๋ ๋ฐ์๋ง ์ง์คํ๊ณ , Model & Controller๋ Business logic๊ณผ ๊ด๋ จ์๊ฑฐ๋, ๋ด๋ถ์ ์ธ ๊ฒ๋ค์ ์ฒ๋ฆฌํ๋๋ฐ ์ง์คํ๊ธฐ ์ํด ์ด๋ฅผ ๋ถ๋ฆฌํ์๋ค.
-
!๋ฉด์ ์ง๋ฌธ!
- ์ ์ง๋ณด์์ฑ
- ์ ํ๋ฆฌ์ผ์ด์ ์ ํ์ฅ์ฑ
- ์ ์ฐ์ฑ ์ฆ๊ฐ(ํด๋ผ์ด์ธํธ์ ์๋ก์ด ์๊ตฌ์ฌํญ์ ๋ํด ์ต์ํ์ ๋น์ฉ์ผ๋ก ๋ณด๋ค ์ ์ฐํ๊ฒ ๋์ฒ)
- ์ค๋ณต์ฝ๋ฉ์ ๋ฌธ์ ์ ๊ฐ์
- ๊ทธ๋ ์ง ์์ผ๋ฉด ์ ์ง, ๋ณด์์ ์์ฒญ๋ ์๊ฐ์ด ํ์ํ ๊ฒ์ด๋ค ...
-
์ ์ ์ปจํ ์ธ ์ ๋ค๋ฅธ ์ ์๋ฒ์์ ๋ฟ๋ ค์ง ์ปจํ ์ธ ์ ๋์ ์ผ๋ก ๋ณํ๋ฅผ ์ค ์ ์๋ค.
์ด๋ฒ์๋ MVC๋ฅผ ์ด์ฉํ์ฌ Parameter๋ฅผ ๋ฐ๋ Controller๋ฅผ ์์ฑํด๋ณด์.
controller/HelloController.java
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam ("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
@RequestParamannotation์ HTTP request parameter๋ฅผ method์ parameter๋ก ์ ๋ฌ๋ฐ์ ๋ ์ฌ์ฉํ๋ค.form์์์input์name๊ณผ ๋งค์นญ๋์ด ํด๋น ๊ฐ์ ๋ฐ์์ฌ ์ ์๋ค.String name์ ํด๋น ๊ฐ์ ๋ด๊ณ ,Model model์ ์ด์ฉํ์ฌView์์ rendering ์ ์ฌ์ฉmodel.addAttributemethod๋กkey๊ฐ "name" ์ด๊ณ ,๋ณ์์ด๋ฆ์ดname์ธ ๊ฐ์ ๊ฐ์ ธ์์View๋ก ์ ๋ฌํ๋ค.
resources/templates/hello-templates.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello' + ${name}">hello! empty</p>
</body>
</html>
-
xmlns:th="~"๋กthymeleafํ ํ๋ฆฟ์ ์ฌ์ฉํจ์ ๋ช ์- thymeleaf์ ์ฅ์ ์, ์์ ๊ฐ์ด ์ ์ฅํ ํ์ผ ์์ฒด๋ฅผ ์๋ฒ์์ด ์ด์ด๋ ํด๋น ํ์ผ์ ์์ค์ฝ๋๋ฅผ ๊ทธ๋๋ก ํ์ธ ๊ฐ๋ฅํ๋ค.
- ์๋ฒ๊ฐ ๋์ํ๊ฒ ๋๋ฉด,
th:text์ ์๋ ๊ฐ์ผ๋กhello! empty๊ฐ ์นํ๋๋ค.
์๋ฒ๋ฅผ ๋์์์ผ http://localhost:8080/hello-mvc ๋ก ์ ์

WARN 51004 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'name' for method parameter type String is not present]
- ์์ ๊ฐ์ log์ ํจ๊ป Error page๊ฐ ๋์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ์ฝ๊ฒ ๋งํด
nameparameter์ ์๋ฌด๊ฒ๋ ๋๊ฒจ์ฃผ์ง ์์์ ๋จ๋ ์๋ฌ๋ผ๊ณ ํ ์ ์๋ค.
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam (name = "name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
- ์์ ๊ฐ์ด, parameter์ ์ด๋ฆ์ ๋ช
์ํ๊ณ ,
requiredoption์ ๊ธฐ๋ณธ๊ฐ์ดtrue์ด๋ฏ๋ก ๊ทธ๋๋ก ๋๋ค.
์ด์ http://localhost:8080/hello-mvc?name=spring ์ผ๋ก ์ ์ํด๋ณด์.

-
๋์๋ฐฉ์์,
- url์์ ๋ฐ์์จ name์ ๋ํ "spring" ์ด๋ผ๋ ๊ฐ์ด
public Stirng helloMvc์String name์ ๋ด๊ธฐ๊ณ , model.addAttribute๋ฅผ ํตํดname์ดmodel์ ๋ด๊ฒจ,hello-template์ผ๋ก return${name}์ผ๋ก,model์์ key๊ฐ์ดname์ธ ๊ฐ์ ๊ฐ์ ธ์์ ์นํ!
- url์์ ๋ฐ์์จ name์ ๋ํ "spring" ์ด๋ผ๋ ๊ฐ์ด
๐ API
JSON์ด๋ผ๋ ๋ฐ์ดํฐ ํฌ๋งท์ผ๋ก ์๋ฒ์์ ํด๋ผ์ด์ธํธ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ ๋ฐฉ์
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
return "hello" + name;
}
@ResponseBody๋, http์์์ body์ ํด๋น ๋ด์ฉ์ ์ง์ ๋ฃ๊ฒ ๋ค๋ ์๋ฏธ!-
Template engine๊ณผ ๋ค๋ฅธ ์ ์ "view" ๊ฐ๋ ์์ด data๊ฐ ๊ทธ๋๋ก ๋ณด์ฌ์ง๋ค.
- ํ์ธํ๊ธฐ ์ํด, http://localhost:8080/hello-string?name=spring ์ผ๋ก ์ ์ํ๋ฉด, ์๋์ ๊ฐ์ ํ๋ฉด์ด ๋ณด์ด๊ณ ,
ํ์ด์ง ์์ค ๋ณด๊ธฐํ๋ฉด, html code ์์ด Data๋ง ์กด์ฌํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.


- ํ์ธํ๊ธฐ ์ํด, http://localhost:8080/hello-string?name=spring ์ผ๋ก ์ ์ํ๋ฉด, ์๋์ ๊ฐ์ ํ๋ฉด์ด ๋ณด์ด๊ณ ,
๋ค์ ์์ ๋ Hello ๊ฐ์ฒด์ getName & setName, ๊ทธ๋ฆฌ๊ณ ๊ฐ์ฒด๋ฅผ return ํ๋ Api ๋ฐฉ์์ด๋ค.
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) {
Hello hello = new Hello();
hello.setName(name);
return hello;
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- http://localhost:8080/hello-api?name=spring ์ผ๋ก ์ ์ํ๋ฉด ์๋ ์ฌ์ง์์ ๋ณด์ด๋ ๋ฐ์ ๊ฐ์ด
Key&Value๋ก ์ด๋ฃจ์ด์ง JSON ๊ตฌ์กฐ๋ฅผ ํ์ธํ ์ ์๋ค.
mac ๊ธฐ์ค
โ + n์ผ๋ก Getter์ Setter๋ฅผ ์์ฑํ ์ ์๋ค.HelloClass ์์ ์๋String name์private์ด๋ฏ๋ก, ํด๋น ๋ณ์์ ๋ํ ์ ๊ทผ์ ์ค์งpublicํgetNameorsetName์ผ๋ก๋ง ๊ฐ๋ฅํ๊ฒ ํ๋ค. โ JavaBeans ํ์ค ๋ฐฉ์
- ์ด์ ๊ฐ์ด
@ResponseBody๋ฅผ ์ฌ์ฉํ ๋์๋ฐฉ์์, - localhost:8080/hello-api๋ก ์ด๋ํ์ฌ controller๊ฐ ์ด๋ฅผ ํ์ธ
- template engine์ ๊ฒฝ์ฐ, ํด๋นํ๋ view๋ฅผ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด viewResolver์๊ฒ ์ ๋ฌํ์ง๋ง
@ResponseBody๋ฅผ ๋ฐ๊ฒฌํ๋ฉดhttp์๋ต์ ํด๋น Data๋ฅผ ๊ทธ๋๋ก ์ ๋ฌํ๊ธฐ ์ํ ์ฒ๋ฆฌ๋ฅผ ์ํ -
์ด๋, ์ ๋ฌํ๋ ค๋ Data๊ฐ ๋ฌธ์์ด์ด ์๋ ๊ฐ์ฒด์ด๋ฏ๋ก ์ด์ ๋ํ ์ฒ๋ฆฌ๊ฐ ํ์ํ๋ค.
- Spring์ ๊ธฐ๋ณธ ์ ์ฑ ์ ๊ฐ์ฒด๊ฐ ์ค๋ ๊ฒฝ์ฐ, ์ด๋ฅผ JSON ๋ฐฉ์์ผ๋ก Http ์๋ต์ ๋ฐํํ๋ ๊ฒ
-
ViewResolver๋์HttpMessageConverter๊ฐ ๋์ํ์ฌ String์ธ ๊ฒฝ์ฐ๋StringConverter, ๊ฐ์ฒด์ธ ๊ฒฝ์ฐ๋JsonConverter๋ฅผ ๋์์ํด- ๋ํ์ ์ธ JsonConverter library๋ก๋
MappingJacksonHttpMessageConveter์GsonHttpMessageConveter๊ฐ ์๋ค. - HttpMessageConverter๋ ํด๋ผ์ด์ธํธ๊ฐ ์ํ๋ response ํํ๋ฅผ header์ ๋ช ์ํ ๊ฒฝ์ฐ, ์ด์ ๋ง์ถฐ ์ ํ๋๋ค. ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ๋ spring์ด ์์์ ์ ํ!
- MessageConverter์ ๋ํ ์์ธํ ์ ๋ณด๋ ์ฐธ๊ณ ์ ์๋ HttpMessageConverter ๋ฅผ ํ์ธ
- ๋ํ์ ์ธ JsonConverter library๋ก๋
- Converter๋ฅผ ์ด์ฉํด ๋ณํ๋ ๊ฒฐ๊ณผ๋ฅผ ์์ฒญ์ธก์ผ๋ก ์ ๋ฌ
์์ ์๋ JSON์ด ์๋ XML ๋ฐฉ์์ ์ฌ์ฉํ์๋๋ฐ, (ex. html code) ์ด๋ ์ด๊ณ ๋ซ๋ ์ด์ค์ code๊ฐ ํ์ํ๊ณ , ๋ฌด๊ฒ๋ค๋ ๋จ์ ๋๋ฌธ์ ์ต๊ทผ์๋ Simpleํ JSON ํํ๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ค.
๐ ์ค์ํ ๊ฐ๋
Template engine, API, Annotation, HttpMessageConverter, JavaBeans